package comchifaner.util;

public class NumberUtil {
	/**
	 * 获取随机ID
	 * 
	 * @return
	 */
	public static long getKey() {
		long key = System.currentTimeMillis();

		key = key * 10;

		int a = (int) (Math.random() * 10);
		key += a;
		return key;
	}

	/**
	 * 获取指定长度的随机字符串
	 * 
	 * @param len
	 * @return
	 */
	public static String getRandomStr(int len) {
		char[] chars = new char[62];
		for (int i = 0; i < 10; i++) {
			chars[i] = (char) ('0' + i);
		}
		for (int i = 0; i < 26; i++) {
			chars[10 + i] = (char) ('a' + i);
			chars[36 + i] = (char) ('A' + i);
		}

		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < len; i++) {
			int a = (int) (Math.random() * 62);
			sb.append(chars[a]);
		}
		return sb.toString();
	}

	/**
	 * 将字符串转换成整型数
	 * 
	 * @return
	 */
	public static int parseInt(Object obj, int defaultVal) {
		int result = defaultVal;
		try {
			if (obj != null && obj.toString().length() > 0) {
				result = Integer.parseInt(obj.toString());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 将字符串转换成长整型数
	 * 
	 * @return
	 */
	public static long parseLong(Object obj, long defaultVal) {
		long result = defaultVal;

		try {
			if (obj != null && obj.toString().length() > 0) {
				result = Long.parseLong(obj.toString());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public static float parseFloat(String obj, float defaultVal) {
		float result = defaultVal;

		try {
			if (obj != null && obj.toString().length() > 0) {
				result = Float.parseFloat(obj);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public static double parseDouble(String obj, double defaultVal) {
		double result = defaultVal;

		try {
			if (obj != null && obj.toString().length() > 0) {
				result = Double.parseDouble(obj);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
}
